//
//  TTAppItemView.m
//  WelfareManager
//
//  Created by timtian on 16/12/31.
//  Copyright © 2016年 timtian. All rights reserved.
//

#import "TTAppItemView.h"

#import "CustomModel.h"
#import "CustomView.h"
#import "UIControl+LMRepeat.h"

#define kImgWH 125 * __kwRatio

@implementation TTAppItemView

// 在initWithFrame:方法中添加子控件
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}



/**
 自定义应用

 @param imageArr 图片资源
 @param nameArr btn名称
 @param columns 列数
 @param tagId btn的tag初始值
 @param target  target
 @param action 动作
 */
-(void)addSubViewImageArr:(NSMutableArray*)imageArr
                 withName:(NSMutableArray*)nameArr
              withColumns:(int)columns
                   withid:(NSInteger)tagId
                addTarget:(id)target
                   action:(SEL)action{
    
    // 每个btn之间的间距 = (控制器view的宽度 - 列数 * 表情的宽度) / (列数 + 1)
    CGFloat margin = (__kScreenWidth - columns * kImgWH) / (columns + 1);
    // 2.定义第一个btn的位置
    // 第一个表情的Y值
    CGFloat oneY = 0;
    // 第一个表情的x值
    CGFloat oneX = margin;
    if (self.itemCount) {
        self.itemCount = 3;
    }
    
    for (int i=0; i<self.itemCount; i++) {
        // i这个位置对应的列数
        int col = i % columns;
        // i这个位置对应的行数
        int row = i / columns;
        // 列数（col）决定了x
        CGFloat x = oneX + col * (kImgWH + margin);
        // 行数（row）决定了y
        CGFloat y = oneY + row * (kImgWH + margin);
        
        CustomView *customView = [[CustomView alloc] init];
        
        customView.backgroundColor = [UIColor whiteColor];
        
        customView.userInteractionEnabled = YES;
        CustomModel *model = [CustomModel modelWithName:nameArr[i] icon:imageArr[i]];
        customView.model = model;
    
        
        customView.frame = CGRectMake(x, y, 125*__kwRatio-0.5, 125*__kwRatio-0.5);
        
        [self addSubview:customView];
        UIControl* tagContr=[[UIControl alloc]initWithFrame:customView.bounds];
//        tagContr.lm_repeatInterval = 1.0;
        [tagContr addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        tagContr.tag=tagId+i;
        [customView addSubview:tagContr];
    }
    
}



@end
